﻿/**
 * PvZ Tool
 * 
 * @classname   Tool
 * @description Code for main window form
 * @author      Martomo
 * @author      Kamizoom
 * @version     0.0.2
 * 
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PvZTool
{

    public partial class Tool : Form
    {
        public Offset Data = new Offset();

        public Tool()
        {
            InitializeComponent();
        }

        private void LoadTool(object sender, EventArgs e)
        {
            // Generate/populate form
            this.SelectLevel.DataSource = new BindingSource(Data.levelList, null);
            this.SelectLevel.DisplayMember = "Value";
            this.SelectLevel.ValueMember = "Key";
            GenerateAchievements();

            // Set default values
            ResetValues();

            // Add event handlers when all components have been created/loaded
            this.SelectLevel.SelectedIndexChanged += new System.EventHandler(this.ChangeLevel);
        }

        private void ExitTool(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void AboutTool(object sender, EventArgs e)
        {
            Function.MsgBox(Program.appName +" v"+ Program.appVersion+"\ncreated by Martomo and Kamizoom", "About");
        }

        private void OpenSave(object sender, EventArgs e)
        {
            if (Save.Open() == true)
            {
                LoadValues();
                ChangeMenuState("close");
            }
        }

        private void CloseSave(object sender, EventArgs e)
        {
            Save.Close();
            ResetValues();
            ChangeMenuState("open");
        }

        private void WriteSave(object sender, EventArgs e)
        {
            if (Save.Write() == true)
            {
                Function.MsgBox("Game has been saved", "Saved");
            }
        }

        private void ChangeMenuState(string status = "open")
        {
            switch (status)
            {
                case "close":
                    this.OpenItem.Text = "&Close";
                    this.SaveItem.Enabled = true;
                    this.OpenItem.Click -= new System.EventHandler(this.OpenSave);
                    this.OpenItem.Click += new System.EventHandler(this.CloseSave);
                    break;
                case "open":
                default:
                    this.Text = Program.appName;
                    this.OpenItem.Text = "&Open";
                    this.SaveItem.Enabled = false;
                    this.OpenItem.Click -= new System.EventHandler(this.CloseSave);
                    this.OpenItem.Click += new System.EventHandler(this.OpenSave);
                    break;
            }
        }

        private void LoadValues()
        {
            this.SelectLevel.Enabled = true;
            // Get current level value from save
            int indexLevel = Save.GetValue(Data.posLevel);

            // Output
            Debug.WriteLine("Int: " + indexLevel);
            Debug.WriteLine("Hex: " + indexLevel.ToString("X2"));
            Debug.WriteLine("Level: " + Data.levelList[indexLevel]);

            // Check if level value is valid
            if (Data.levelList.ContainsKey(indexLevel))
            {
                this.SelectLevel.SelectedValue = indexLevel;
            }
        }

        private void ResetValues() {
            this.SelectLevel.SelectedIndex = -1;
            this.SelectLevel.Text = "";
            this.SelectLevel.Enabled = false;
        }

        private void ChangeLevel(object sender, EventArgs e)
        {
            // Output
            Debug.WriteLine("- LevelChange -");
            Debug.WriteLine("Index: " + SelectLevel.SelectedIndex);
            Debug.WriteLine("Item: " + SelectLevel.SelectedItem);
            Debug.WriteLine("Text: " + SelectLevel.SelectedText);
            Debug.WriteLine("Value: " + SelectLevel.SelectedValue);

            if (SelectLevel.SelectedIndex > -1)
            {
                // Get index from selectLevel comboBox
                int indexLevel = (int)this.SelectLevel.SelectedValue;
                // Store change in Save.saveChanges
                Save.SetChange(Data.posLevel, indexLevel);
            }
        }

        private void TabResize(object sender, EventArgs e)
        {
            switch (this.ContentTabs.SelectedTab.Text)
            {
                case "Achievements":
                    this.Size = new System.Drawing.Size(590, 450);
                    this.ContentTabs.Size = new System.Drawing.Size(550, 363);
                    break;
                default:
                    this.Size = new System.Drawing.Size(300, 300);
                    this.ContentTabs.Size = new System.Drawing.Size(260, 213);
                    break;
            }
            Debug.WriteLine(this.ContentTabs.SelectedIndex + " - " + this.ContentTabs.SelectedTab.Text);
        }

        public void GenerateAchievements()
        {
            int fixY = 20;
            int posY = fixY;
            int posX = 20;
            int cWidth = 180;
            int cHeight = 17;
            int columnCount = 1;
            int columnLimit = 16;
            var tabID = TabAchievement;
            int changeIndex = 0;

            CheckBox[] chkBox = new CheckBox[Data.achievementList.Count];

            Debug.WriteLine("Achievements: " + Data.achievementList.Count);
            Debug.WriteLine("chkBox: " + chkBox.Length);

            // Start looping through Achievements
            foreach (KeyValuePair<string, int[]> change in Data.achievementList)
            {
                Debug.WriteLine("Column: #{0}, Y{1}, X{2}", columnCount, posY, posX);
                if (columnCount == columnLimit)
                {
                    // next row
                    Debug.WriteLine("Next Row");
                    posY = fixY;
                    posX += cWidth;
                    columnCount = 1;
                }

                // Create checkbox here
                chkBox[changeIndex] = new CheckBox();
                chkBox[changeIndex].Name = change.Key;
                chkBox[changeIndex].Text = change.Key;
                chkBox[changeIndex].Location = new System.Drawing.Point(posX, posY);
                chkBox[changeIndex].Size = new System.Drawing.Size(cWidth, cHeight);
                chkBox[changeIndex].Visible = true;
                chkBox[changeIndex].Parent = tabID;
                posY += 20;
                columnCount++;

                // Output
                Debug.WriteLine("Value #{0}: {1}, {2}",
                changeIndex,
                change.Key,
                change.Value);
                changeIndex++;
            }
        }
    }
}
